{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Executing code in a remote process\n",
    "\n",
    "Suppose we wanted to use some functions defined in the following q process: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/ Open another q process\n",
    "system $[.z.o like\"w*\";\"start \";\"\"],\"q rmt.q -p 4044\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The file ``rmt.q`` contains the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\\\n",
    "/ run the cell to see the content of the file\n",
    "/%loadscript rmt.q\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This allows to call the functions in the process either synchronously or asynchronously using a handle:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "/ Open connection\n",
    "h:hopen`::4044\n",
    "\n",
    "/ Synchronous\n",
    "h\"add . 2 3\"\n",
    "/Asynchronous\n",
    "neg[h]\"add . 3 3\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition, as the script ```jupyterq_remote.q``` is loaded in the remote process, ```/%remote handle``` allows to run code in the remote process easily:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/%remote h\n",
    "hello:f(\"Hello\";\"World\")\n",
    "table:rndtab 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And completion or help can be used as if the remote process was the server process the notebook is running:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/%remote h\n",
    "ad   / place your cursor anywhere in the word and hit tab\n",
    "tab    / if you place your cursor in this word and hit TAB you should see a drop down with table and tables\n",
    "rndtab / if you place your cursor anywhere and hit SHIFT TA, you should obtain info about the function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variable ``hello`` in the remote process should contain the string \"Hello World\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello World\"~h\"hello\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And ``table`` is a table that we can query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/%remote h\n",
    "select from table where sym=`A\n",
    "select avg price by sym from table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or load in the notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "10#t:h\"table\"\n",
    "select from t where price<15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/ Close q process\n",
    "neg[h](exit;0);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Q (kdb+)",
   "language": "q",
   "name": "qpk"
  },
  "language_info": {
   "file_extension": ".q",
   "mimetype": "text/x-q",
   "name": "q",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
